package com.finalist.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import com.finalist.entity.BossArea;
import com.finalist.entity.BossArea.Type;

public class ReadExcelUtils
{

    public static void ReadSectionExcel(InputStream is, List<BossArea> bossAreaList)
    {
        try
        {
            jxl.Workbook rwb = Workbook.getWorkbook(is);
            //int sheetNum = rwb.getNumberOfSheets();
            int sheetNo = 0;
            //            for (int sheetNo = 0; sheetNo < sheetNum; sheetNo++)
            //            {
            Sheet rs = rwb.getSheet(sheetNo);
            //总行数
            int row = rwb.getSheet(sheetNo).getRows();
            //String name = rs.getName();
            //取第一行，第一列的元素 
            for (int i = 1; i < row; i++)
            {
                Cell c00 = rs.getCell(0, i);//城市区号              
                String strc00 = c00.getContents();

                Cell c01 = rs.getCell(1, i);//上级地区ID
                String strc01 = c01.getContents();

                Cell c02 = rs.getCell(2, i);//城市名称
                String strc02 = c02.getContents();

                Cell c03 = rs.getCell(3, i);//号段
                String strc03 = c03.getContents();

                Cell c04 = rs.getCell(4, i);//运营商类型(0:移动 1：联通 2：电信)
                String strc04 = c04.getContents();

                BossArea tempBossArea = new BossArea();
                tempBossArea.setCode(strc00);//编码
                tempBossArea.setName(strc02);
                BossArea parentBossArea = new BossArea();
                parentBossArea.setId(Long.valueOf(strc01));
                tempBossArea.setParent(parentBossArea);
                tempBossArea.setNoPrefix(strc03);

                if ("0".equals(strc04))
                {
                    tempBossArea.setType(Type.CMCC);
                }
                else if ("1".equals(strc04))
                {
                    tempBossArea.setType(Type.CUCC);
                }
                else if ("2".equals(strc04))
                {
                    tempBossArea.setType(Type.CTCC);
                }
                bossAreaList.add(tempBossArea);

            }
            //            }

        }
        catch (Exception ex)
        {
        }

    }


    /*
    *数字不足位数左补0
    *
    * @param str
    * @param strLength
    */
    public static String addZeroForNum(String str, int strLength)
    {
        int strLen = str.length();
        if (strLen < strLength)
        {
            while (strLen < strLength)
            {
                StringBuffer sb = new StringBuffer();
                sb.append("0").append(str);//左补0
                // sb.append(str).append("0");//右补0
                str = sb.toString();
                strLen = str.length();
            }
        }
        return str;
    }

    //将MultipartFile 转换为File
    @SuppressWarnings("unused")
    public static void SaveFileFromInputStream(InputStream stream, String path, String savefile) throws IOException
    {
        File tempDir = new File(path);
        if (!tempDir.exists())
        {
            tempDir.mkdirs();
        }

        FileOutputStream fs = new FileOutputStream(path + "/" + savefile);
        System.out.println("------------" + path + "/" + savefile);
        byte[] buffer = new byte[1024 * 1024];
        int bytesum = 0;
        int byteread = 0;
        while ((byteread = stream.read(buffer)) != -1)
        {
            bytesum += byteread;
            fs.write(buffer, 0, byteread);
            fs.flush();
        }
        fs.close();
        stream.close();
    }

    //    public static void main(String[] args) throws FileNotFoundException
    //    {
    //        InputStream is = new FileInputStream("d:\\号码数据.xls");
    //        List<BossArea> bossAreaList = new ArrayList<BossArea>();
    //        List<NeusoftPhoneNumber> neusoftPhoneNumberList = new ArrayList<NeusoftPhoneNumber>();
    //        ReadExcel(is, bossAreaList, neusoftPhoneNumberList);
    //    }
}
